package com.cqggrw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqggrw.dao.BookDaoI;
import com.cqggrw.domain.Book;
import com.cqggrw.util.DBUtil;
import com.cqggrw.util.Utils;

public class BookDaoImpl implements BookDaoI {

	public boolean add(Book book) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_book(name, addDate, peopleName, fmImg, des) values(?, ?, ?, ?, ?);";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, book.getName());
			ps.setString(2, book.getAddDate());
			ps.setString(3, book.getPeopleName());
			ps.setString(4, book.getFmImgFileName());
			ps.setString(5, book.getDes());
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public boolean del(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_book where id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public boolean modify(Book book) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "update tbl_book set name = ?, addDate = ?, peopleName = ?, fmImg = ?, des = ? WHERE ID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, book.getName());
			ps.setString(2, book.getAddDate());
			ps.setString(3, book.getPeopleName());
			ps.setString(4, book.getFmImgFileName());
			ps.setString(5, book.getDes());
			ps.setInt(6, book.getId());
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public Book get(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Book book = new Book();
		
		String sql = "SELECT * FROM tbl_book where id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				book.setId(id);
				book.setName(rs.getString("name"));
				book.setAddDate(rs.getString("addDate"));
				book.setPeopleName(rs.getString("peopleName"));
				book.setFmImgFileName(rs.getString("fmImg"));
				book.setDes(rs.getString("des"));
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return book;
	}

	public List<Book> get(Book book) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Book b = null;
		List<Book> list = new ArrayList<Book>();
		
		String sql = "SELECT * FROM tbl_book ORDER BY ADDDATE desc limit ?, ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (book.getPage()-1)*book.getRows());
			ps.setInt(2, book.getRows());
			rs = ps.executeQuery();
			while (rs.next()) {
				b = new Book();
				b.setId(rs.getInt("id"));
				b.setName(rs.getString("name"));
				b.setPeopleName(rs.getString("peopleName"));
				b.setAddDate(rs.getString("addDate"));
				b.setFmImgFileName(rs.getString("fmImg"));
				b.setDes(rs.getString("des"));
				list.add(b);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	public int getTotal() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int total = 0;
		
		String sql = "select count(*) from tbl_book";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return total;
	}

}
